package com.cjh.lms.json;

import java.util.List;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;
import com.cjh.lms.exception.EncryptException;
import com.cjh.lms.json.DateToJsonUtil.GsonBuilderUtil;
import com.cjh.lms.tool.AES.AES;

public class AppResponse {
	
	public final Boolean isEncrypt = true;
	JSONObject unEncryptData = new JSONObject();//put非加密data区
	JSONObject encryptData = new JSONObject();//put加密data区
	JSONObject unEncrypt = new JSONObject();//put非加密公共区
	JSONObject encrypt = new JSONObject();//put加密公共区
	JSONObject resultData = new JSONObject();//整个data区
	JSONObject resultJson = new JSONObject();//所有包含结果码
	Object encryptList;//list区域
//	Map<String, Object> resultData = new HashMap<>();
//	Map<String, Object> encrypt = new HashMap<>();
//	Map<String, Object> unEncrypt = new HashMap<>();
//	Map<String, Object> encryptData = new HashMap<>();
//	Map<String, Object> unEncryptData = new HashMap<>();
//	Map<String, Object> resultJson = new HashMap<>();
	

	public AppResponse() {
		encrypt.put("r", getRandomString(2));
	}
	public AppResponse(int state,int errorCode) {
		encrypt.put("r", getRandomString(2));
		resultJson.put("state", state);
		resultJson.put("errCode", errorCode);
	}
	

	public AppResponse putUnEncrypt(String key, Object value) {
		unEncryptData.put(key, value);
		return this;
	}

	public AppResponse putEncrypt(String key, Object value) {
		encryptData.put(key, value);
		return this;
	}
	
	public AppResponse putList(List<?> list){
		encryptList = JSONObject.toJSON(list);
		return this;
	}
	
	public AppResponse putResult(int state,int errorCode){
		resultJson.put("state", state);
		resultJson.put("errCode", errorCode);
		return this;
	}
	public AppResponse putSuccessResult(){
		resultJson.put("state", 0);
		resultJson.put("errCode", 0);
		return this;
	}
	public Object toJsonString(String aesKey) throws EncryptException{
		return toJsonString( true , aesKey);
	}
	public Object toJsonString() throws EncryptException{
		return toJsonString( false , null);
	}
	public Object toErrorString(){
		unEncrypt.put("data", unEncryptData);
		encrypt.put("data", encryptData);
		resultData.put("unEncrypt", unEncrypt);	
		resultData.put("encrypt", encrypt);
		resultJson.put("data", resultData);
		return JSONObject.toJSON(resultJson);
	}
	public Boolean isSuccess(){
		
		return (this.resultJson.getInteger("state")!=null)&&(this.resultJson.getInteger("state")==0);
	}
	private Object toJsonString(Boolean isUseKey,String aesKey) throws EncryptException{
		unEncrypt.put("data", unEncryptData);
		if(isEncrypt){
			encrypt.put("data", encryptData);
			if(encryptList != null)
				encrypt.put("list", encryptList);		
		}else{
			if(encryptList != null)
				unEncrypt.put("list", encryptList);
		}
		if(aesKey==null||aesKey.isEmpty()){
			if(isUseKey){
				resultJson.put("result", 01);//加密失敗
				resultJson.put("result", 01001);//密钥为空
			}
			resultData.put("encrypt", "");
		}else{
			try {
				resultData.put("encrypt", AES.encrypt(aesKey, GsonBuilderUtil.create().toJson(encrypt)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				throw new EncryptException("10002");
			}
		}	
		resultData.put("unEncrypt", unEncrypt);	
		resultJson.put("data", resultData);

		return JSONObject.toJSON(resultJson);
	}

	private String getRandomString(int length) {
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString(); 
	}

}
